import java.util.Optional;
import java.util.function.Predicate;

/**
 * @author zhouyanxiang
 * @Date 2020-11-2020/11/23-14:46
 */
public class Main {

    public static void main(String[] args) {
        Main m = new Main();

        Integer v1 = null;
        Integer v2 = 23;
        Integer v3 = 0;
        // Optional.ofNullable - 允许传递为 null 参数
        Optional<Integer> a = Optional.ofNullable(v1);
        Optional<Integer> b = Optional.of(v2);

        // isPresent()方法和isEmpty()功能相反
        if (!a.isPresent()) {
            // 如果v1为空那么就取v2的值
             v3 = Optional.ofNullable(v1).orElse(v2);
        }
        System.out.println(v3);
        System.out.println(b);

        String name = "Tom";
        Optional<String> nameOptional = Optional.of(name);
        // map映射转换
        Optional<Integer> intOpt = nameOptional.map(String::length);
        System.out.println( intOpt.orElse(0));

        String password = "password";
        Optional<String>  opt = Optional.ofNullable(password);

        Predicate<String> len6 = pwd -> pwd.length() > 6;
        Predicate<String> len10 = pwd -> pwd.length() < 10;
        Predicate<String> eq = pwd -> pwd.equals("password");

        // 检查opt长度和值
        boolean result = opt.map(String::toLowerCase).filter(len6.and(len10 ).and(eq)).isPresent();
        System.out.println(result);
    }

}
